﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Common
{
    public class Data
    {
        #region Reflection

        /// <summary>
        /// Clase para obtener valores por Reflection
        /// </summary>
        public static class Reflection
        {
            /// <summary>
            /// Obtiene el valor de la propiedad de un objeto, dado el objeto y el nombre de la propiedad
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="src"></param>
            /// <param name="propertyName"></param>
            /// <returns></returns>
            public static T GetPropertyValue<T>(object src, string propertyName)
            {
                return (T)src.GetType().GetProperty(propertyName).GetValue(src, null);
            }
        }

        #endregion

        #region Pagination

        public class Pagination
        {
            #region Constructors

            /// <summary>
            /// <param name="totalRecords"></param>
            /// <param name="totalPages"></param>
            /// <param name="currentPage"></param>
            /// <param name="pageSize"></param>
            /// Constructor
            /// </summary>
            /// <param name="sortOrder"></param>
            /// <param name="sortColumn"></param>
            public Pagination(int totalRecords, int totalPages, int currentPage, int pageSize, string sortOrder, string sortColumn)
            {
                this.TotalRecords   = totalRecords;
                this.TotalPages     = totalPages;
                this.CurrentPage    = currentPage;
                this.PageSize       = pageSize;
                this.SortOrder      = sortOrder;
                this.SortColumn     = sortColumn;
            }

            #endregion

            #region Properties

            private int _TotalRecords;
            /// <summary>
            /// Total records
            /// </summary>
            public int TotalRecords
            {
                get { return _TotalRecords; }
                set { _TotalRecords = value; }
            }

            private int _TotalPages;
            /// <summary>
            /// Total pages
            /// </summary>
            public int TotalPages
            {
                get { return _TotalPages; }
                set { _TotalPages = value; }
            }

            private int _CurrentPage;
            /// <summary>
            /// Current page
            /// </summary>
            public int CurrentPage
            {
                get { return _CurrentPage; }
                set { _CurrentPage = value; }
            }

            private int _PageSize;
            /// <summary>
            /// Page size 
            /// </summary>
            public int PageSize
            {
                get { return _PageSize; }
                set { _PageSize = value; }
            }

            private string _SortOrder;
            /// <summary>
            /// Sort order
            /// </summary>
            public string SortOrder
            {
                get { return _SortOrder; }
                set { _SortOrder = value; }
            }

            private string _SortColumn;
            /// <summary>
            /// Sort order
            /// </summary>
            public string SortColumn
            {
                get { return _SortColumn; }
                set { _SortColumn = value; }
            }

            #endregion
        }

        #endregion

        #region Parameters
        
        public class ParameterList
        {
            #region Constructors
            public ParameterList()
            {
                this.List = new List<KeyValuePair<string, object>>();
            }
            #endregion

            #region Private Members
            private readonly List<KeyValuePair<string, object>> List;
            #endregion

            #region Public Methods
            public Dictionary<string, object> ToDictionary()
            {
                return this.List.ToDictionary(keyValuePair => keyValuePair.Key, keyValuePair => keyValuePair.Value);
            }
            public void AddParameter(string key, object value)
            {
                this.List.Add(this.CreateItem(key, value));
            }
            public void AddParameter(string key, bool condition, object trueValue, object falseValue)
            {
                this.List.Add(this.CreateItem(key, condition ? trueValue : falseValue));
            }
            public void AddParameter(string key, object value, int position)
            {
                this.List.Insert(position, this.CreateItem(key, value));
            }
            #endregion

            #region Private Methods
            private KeyValuePair<string, object> CreateItem(string key, object value)
            {
                return new KeyValuePair<string, object>(key, value);
            }
            #endregion
        }

        #endregion

        public class Randomizer
        {
            private static readonly Random rnd = new Random();
            private const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            public static string GenerateString(int size)
            {
                var buffer = new char[size];
                for (var i = 0; i < size; i++)
                {
                    buffer[i] = chars[rnd.Next(chars.Length)];
                }
                return new string(buffer);
            }
        }

    }
}
